package com.qf.business.user.controller;

import com.qf.business.user.service.UserService;
import com.qf.data.base.r.R;
import com.qf.data.base.r.Rs;
import com.qf.data.base.utils.QfBeanUtils;
import com.qf.data.user.entity.User;
import com.qf.data.user.vo.input.WxUserInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * description: 微信端的用户管理
 * author: Ken
 * 公众号：Java架构栈
 */
@RestController
@RequestMapping("/user/wx")
public class WxUserController {

    @Autowired
    private UserService userService;

    /**
     * 微信用户的信息查询
     * @return
     */
    @RequestMapping("/query")
    public R<User> wxUserQuery(@RequestBody @Valid WxUserInput input){
        //将input对象 转换成 entity对象
        User user = QfBeanUtils.beanCopy(input, User.class);
        //查询数据库用的用户信息
        User dataUser = userService.queryByOpenId(user.getOpenId());
        if (dataUser == null) {
            //数据库中没有用户信息，保存当前的用户信息
            userService.save(user);
            return Rs.create(user);
        }
        return Rs.create(dataUser);
    }
}
